import _ from 'lodash';

export function sortFixed(arr) {
  return _.sortBy(arr, [
    (item) => {
      if (!item.fixed) {
        return 0;
      } else if (item.fixed === 'right') {
        return 1;
      } else {
        return -1;
      }
    },
  ]);
}

export function deepClone(obj) {
  return JSON.parse(JSON.stringify(obj));
}

export function rAF(fn) {
  let ticking = false;
  const update = (...arg) => {
    ticking = false;
    fn.apply(this, arg);
  };

  return (...arg) => {
    const self = this;

    if (!ticking) {
      requestAnimationFrame(() => {
        update.apply(self, arg);
      });
    }
    ticking = true;
  };
}

export function debug(option = {}) {
  const prefix = option.prefix || '';
  const disabled = !!option.disabled;

  return (...arg) => {
    if (process.env.NODE_ENV === 'development' && !disabled) {
      if (prefix) {
        arg.unshift('background: #222; color: #bada55');
        arg.unshift(`%c ${prefix}`);
      }
    }
  };
}
